﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Commons;
using IndoReputation.Models.Constants;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.BusinessObject;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class RankTypeController : BaseController
    {
        //
        // GET: /RankType/

        public ActionResult Index()
        {
            ViewData["ajax"] = true;
            ViewData["scrolling"] = true;
            ViewData["paging"] = true;
            ViewData["filtering"] = true;
            ViewData["grouping"] = true;
            ViewData["sorting"] = true;
            ViewData["showFooter"] = true;
            return View(RankTypeBOL.GetAllRankType());            
            //return View();
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(RankTypeBOL.GetAllRankType()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record
                if (!RankTypeBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This RankType have Media. Please check again!");
                }
            }
            //Rebind the grid
            return View(new GridModel(RankTypeBOL.GetAllRankType()));
        }

        [HttpGet]
        public ActionResult Add()
        {            
            return View();
        }

        [HttpPost]
        public ActionResult Add(RankTypeViewModel model)
        {
            if (ModelState.IsValid)
            {
                RankType rankType = new RankType
                {
                    Description = model.Description,
                    Name = model.Name.Trim(),
                    RankValue = Convert.ToByte(model.RankValue)
                };
                RankTypeBOL.Insert(rankType);
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int rankTypeID)
        {
            var rankType = RankTypeBOL.GetRankTypeById(rankTypeID);
            var model = new RankTypeViewModel
            {
                CreateAt = rankType.CreateAt,
                CreateBy = rankType.CreateBy,
                Description = rankType.Description,
                ID = rankType.ID,
                Name = rankType.Name,
                RankValue = rankType.RankValue
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(RankTypeViewModel model)
        {
            if (ModelState.IsValid)
            {
                RankType rankType = new RankType
                {
                    CreateAt = model.CreateAt,
                    CreateBy = model.CreateBy,
                    ID = model.ID,
                    Description = model.Description,
                    Name = model.Name.Trim(),
                    RankValue = Convert.ToByte(model.RankValue)
                };
                RankTypeBOL.Update(rankType);
            }
            return RedirectToAction("Index");
        }
    }
}
